﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forest.Cat.Domain.Entities;
using Forest.Core.Data;
using Forest.Core;
using Forest.Cat.Domain;
using Forest.Web.Framework.Controllers;
using Forest.Cat.Domain.Services.CoverLetters;
using Forest.Cat.Admin.Models.CoverLetters;

namespace Forest.Cat.Admin.Controllers {
    public class CoverLetterController : BaseController {

        #region Fields
        private readonly ICoverLetterService _coverletterService;
        private readonly IWorkContext _workcontext;
        #endregion

        #region Constructors
        public CoverLetterController(ICoverLetterService coverletterService,
            IWorkContext workcontext) {
            _coverletterService = coverletterService;
            _workcontext = workcontext;
        }
        #endregion

        #region Methods
        public ActionResult Index() {
            return RedirectToAction("CoverLetterList");
        }

        public ActionResult CoverLetterList() {
            // permissionservice
            // var coverletters = _coverletterService.GetAllCoverLetters(0, 5);
            var coverletters = _coverletterService.GetCoverLetters(_workcontext.CurrentUser.Id);

            var model = new CoverLetterListModel();
            model.CoverLetters = coverletters.Select(x => {
                var m = x.ToModel();
                return m;
            }).ToList();
            ViewBag.CurrentAction = "CoverLetterList";
            return View(model);
        }

        public ActionResult List2(CoverLetterPagingFilteringModel command) {
            var model = new CoverLetterListModel();

            // 设置默认的Page信息
            if (command.PageSize <= 0)
                command.PageSize = 20;
            if (command.PageNumber <= 0)
                command.PageNumber = 1;

            var coverletters = _coverletterService.GetAllCoverLetters(command.PageNumber - 1, command.PageSize);
            model.PagingFilteringContext.LoadPagedList(coverletters);

            model.CoverLetters = coverletters.Select(x => {
                var m = x.ToModel();
                return m;
            }).ToList();

            return View(model);
        }

        public ActionResult Create() {
            // 判断权限
            //

            var model = new CoverLetterModel() {
                Id = Guid.NewGuid(),
                JobseekerId = _workcontext.CurrentUser.Id
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CoverLetterModel model) {
            // 判断权限
            //

            if (ModelState.IsValid) {
                var letter = model.ToEntity();
                letter.Id = _coverletterService.CreateId;
                letter.DateCreated = DateTime.Now;
                letter.LastUpdated = DateTime.Now;

                _coverletterService.InsertCoverLetter(letter);

                // Success Notification
                return RedirectToAction("CoverLetterList");
            }
            return View(model);

        }

        public ActionResult Edit(Guid id) {
            // 判断权限
            //
            var letter = _coverletterService.GetCoverLetterById(id);
            if (letter == null)
                return RedirectToAction("CoverLetterList");

            var model = letter.ToModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(CoverLetterModel model) {
            // 判断权限
            //

            var letter = _coverletterService.GetCoverLetterById(model.Id);
            if (letter == null)
                return RedirectToAction("CoverLetterList");

            if (ModelState.IsValid) {
                letter = model.ToEntity(letter);

                letter.LastUpdated = DateTime.Now;
                _coverletterService.UpdateCoverLetter(letter);

                // SuccessNotification...
                return RedirectToAction("CoverLetterList");
            }

            return View(model);

        }

        [HttpPost]
        public ActionResult CoverLetterDelete(string deleteId) {
            //
            // permission
            if (!string.IsNullOrWhiteSpace(deleteId)) {
                var entity = _coverletterService.GetCoverLetterById(Guid.Parse(deleteId));
                _coverletterService.DeleteCoverLetter(entity);
            }

            // Success
            ViewBag.CurrentAction = "CoverLetterList";
            return RedirectToAction("CoverLetterList");
        }

        [HttpPost]
        public ActionResult CoverLetterDeleteList(string deleteIds) {
            //
            // permission
            var idList = deleteIds.Split(',').Where(item => !string.IsNullOrWhiteSpace(item));
            foreach (var id in idList) {
                var entity = _coverletterService.GetCoverLetterById(Guid.Parse(id));
                _coverletterService.DeleteCoverLetter(entity);
            }

            // Success
            ViewBag.CurrentAction = "CoverLetterList";
            return RedirectToAction("CoverLetterList");
        }
        #endregion

    }
}
